package com.cw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cw.common.api.ResultObject;
import com.cw.constant.Constants;
import com.cw.entity.OccupationBed;
import com.cw.mapper2.OccupationBedMapper;
import com.cw.service.IOccupationBedLogService;
import com.cw.service.IOccupationBedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  占床业务主表服务实现类
 * </p>
 *
 * @author ly
 * @since 2021-06-10
 */
@Service
public class OccupationBedServiceImpl extends ServiceImpl<OccupationBedMapper, OccupationBed> implements IOccupationBedService {


    private IOccupationBedLogService occupationBedLogService;

    @Autowired
    private void setterAutowiredMethod(IOccupationBedLogService occupationBedLogService) {
        this.occupationBedLogService = occupationBedLogService;
    }

    /**
     * 检查病床是否被使用
     * @param bedNo 床号
     * @return java.Util.Map<java.lang.String,java.lang.Object>
     * @author ly
     * @date 2021/6/10 11:03
     */
    @Override
    public Map<String,Object>
    checkOutTheBedNoForUse(String bedNo) {
        Map<String, Object> resultMap = new HashMap<>(2);

        LambdaQueryWrapper<OccupationBed> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OccupationBed::getBedNo, bedNo);
        //状态为被使用状态
        lambdaQueryWrapper.eq(OccupationBed::getState, Constants.OCCUPATION_BED_USED);
        List<OccupationBed> list = list(lambdaQueryWrapper);

        //true:有人使用  false:未被使用
        boolean flag = !CollectionUtils.isEmpty(list);
        resultMap.put("state",flag);
        //当flag参数为true时 将occupationId返回
        resultMap.put("occupationId", flag ? list.get(0).getId() : null);
        //后面的需求要对象里面的详细的信息。但是我懒得改之前的代码了,就直接传输一个对象过去了
        //别问问就是自己改,可以简单点的 但没有必要
        resultMap.put("OccupationBed",flag ? list.get(0) : null);
        return resultMap;
    }

    /**
     * 取消占床
     * @param occupationId 占床主键
     * @param bedNo 床号
     * @return boolean
     * @author ly
     * @date 2021/6/10 11:23
     */
    @Override
    public boolean unUsedOccupationBed(String occupationId,String bedNo) {
        OccupationBed occupationBed = new OccupationBed();
        occupationBed.setId(occupationId);
        occupationBed.setState(Constants.OCCUPATION_BED_UNUSED);
        //插入日志
        occupationBedLogService.insertDeleteLog(occupationId, bedNo);
        return updateById(occupationBed);
    }

    /**
     * 取消占床
     * @param occupationBed 占床对象
     * @return boolean
     * @author ly
     * @date 2021/6/10 11:23
     */
    @Override
    public boolean unUsedOccupationBed(OccupationBed occupationBed) {
        occupationBed.setState(Constants.OCCUPATION_BED_UNUSED);
        //插入日志
        occupationBedLogService.insertDeleteLog(occupationBed.getId(), occupationBed.getBedNo());
        return updateById(occupationBed);
    }

    /**
     * 新增占床功能
     * @param occupationBed 占床对象
     * @return com.cw.common.api.ResultObject
     * @author ly
     * @date 2021/6/16 14:09
     */
    @Override
    public ResultObject insertOccupationBed(OccupationBed occupationBed) {
        //判断是否被占用
        LambdaQueryWrapper<OccupationBed> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OccupationBed::getState, "0");
        lambdaQueryWrapper.eq(OccupationBed::getBedNo, occupationBed.getBedNo());
        List<OccupationBed> list = list(lambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(list)) { return ResultObject.errorReturn("当前床位已被占床使用"); }

        String occupationBedId = UUIDUtil.uuid();
        occupationBed.setId(occupationBedId);
        occupationBed.setCreateTime(LocalDateTime.now());
        //设定为占床状态
        occupationBed.setState("0");
        boolean save = save(occupationBed);
        if (save) {
            occupationBedLogService.insertLog(occupationBedId, occupationBed.getBedNo(), occupationBed.getOperNo(), occupationBed.getOperName());
        }
        return save ? ResultObject.successReturn("新增成功!") : ResultObject.errorReturn("新增失败!");
    }

    @Override
    public List<OccupationBed> getOccupiedBed(String constants) {
        LambdaQueryWrapper<OccupationBed> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //状态为被使用状态
        lambdaQueryWrapper.eq(OccupationBed::getState, constants);
        return list(lambdaQueryWrapper);
    }

}
